


//
//  WYCBindingIDController.m
//  初试
//
//  Created by 李胜军 on 2017/10/20.
//  Copyright © 2017年 李胜军. All rights reserved.
//

#import "WYCBindingIDController.h"
#import "WYCAccount.h"
#import <SVProgressHUD.h>
#import "RSAUtil.h"
#import "WYCRealnameController.h"

@interface WYCBindingIDController ()
/** 姓名输入框*/
@property(nonatomic,strong)UITextField * nameTexField;

/** 性别输入框*/
@property(nonatomic,strong)UITextField * sexTexField;

/** 性别输入框*/
@property(nonatomic,strong)UITextField * monthTexField;

/** 性别输入框*/
@property(nonatomic,strong)UITextField * adressTexField;

/** 性别输入框*/
@property(nonatomic,strong)UITextField * numberTexField;


/** 民族*/
@property(nonatomic,strong)UITextField * NationField;
@end

@implementation WYCBindingIDController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //界面布局
    [self setupNav];
}
- (void)setupNav{
    
    self.view.backgroundColor = WYCGlobalBg;
    
    UIView * registerView = [[UIView alloc] init];
    registerView.frame = CGRectMake(0, 0, ScreenWidth, NavHeight);
    registerView.backgroundColor = wycMain;
    [self.view addSubview:registerView];
    
    
    //不是Nav。所以自己写的View和label
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(ScreenWidth / 2 - 200 * px, 0, 400 * px, 64);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"绑定身份证";
    [registerView addSubview:NavRigsterLabel];
    
    
    
    //返回按钮
    UIButton *  backbutton = [[UIButton alloc] init];
    [backbutton setImage:[UIImage imageNamed:@"Nav_back"] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    backbutton.frame = CGRectMake(40 * px, NavRigsterLabel.height / 3 - 30 * px, 40 , 40);
    [registerView addSubview:backbutton];
    
    
    
    
    //照片
    
    UIImageView * iphoneimage = [[UIImageView alloc] init];
    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.faceUrl]];
    iphoneimage.image = [UIImage imageWithData:data];
    iphoneimage.frame = CGRectMake(40 * px, CGRectGetMaxY(registerView.frame) + 40 * px, ScreenWidth - 80 * px, 600 * px);
    iphoneimage.contentMode = UIViewContentModeScaleAspectFill;
    iphoneimage.clipsToBounds=YES;//  是否剪切掉超出
//    [iphoneimage setContentScaleFactor:[[UIScreen mainScreen] scale]];
    [self.view addSubview:iphoneimage];
    
    /******************姓名********/
    UILabel * nameLbel = [[UILabel alloc] init];
    nameLbel.text = @"姓名:";
    nameLbel.font = [UIFont systemFontOfSize:49 * px];
    nameLbel.textColor = WYCRGBColor(120, 112, 115);
    nameLbel.frame = CGRectMake(iphoneimage.x, CGRectGetMaxY(iphoneimage.frame) + 80 * px, 150 * px, 60 * px);
    [self.view addSubview:nameLbel];
    
    
    //+1
    UITextField * nameField = [[UITextField alloc] init];
    nameField.frame = CGRectMake(CGRectGetMaxX(nameLbel.frame) + 20 * px, nameLbel.y, ScreenWidth, nameLbel.height);
    nameField.font = [UIFont systemFontOfSize:49 * px];
        nameField.text = self.name;
    nameField.textColor = WYCRGBColor(120, 112, 115);
    [self.view addSubview:nameField];
    self.nameTexField = nameField;
    
    /******************性别********/
    UILabel * sexlabel = [[UILabel alloc] init];
    sexlabel.text = @"性别:";
    sexlabel.font = [UIFont systemFontOfSize:49 * px];
    sexlabel.textColor = WYCRGBColor(120, 112, 115);
    sexlabel.frame = CGRectMake(iphoneimage.x, CGRectGetMaxY(nameLbel.frame) + 80 * px, nameLbel.width, 60 * px);
    [self.view addSubview:sexlabel];
    
    
    //性别
    UITextField * sexField = [[UITextField alloc] init];
    sexField.frame = CGRectMake(CGRectGetMaxX(sexlabel.frame) + 20 * px, sexlabel.y, ScreenWidth, sexlabel.height);
    sexField.font = [UIFont systemFontOfSize:49 * px];
    sexField.enabled = NO;
    sexField.text = self.sex;
    sexField.textColor = WYCRGBColor(120, 112, 115);
    [self.view addSubview:sexField];
    self.sexTexField = sexField;
    
    
    
    /******************民族********/
    UILabel * Nationlabel = [[UILabel alloc] init];
    Nationlabel.text = @"民族:";
    Nationlabel.font = [UIFont systemFontOfSize:49 * px];
    Nationlabel.textColor = WYCRGBColor(120, 112, 115);
    Nationlabel.frame = CGRectMake(iphoneimage.x, CGRectGetMaxY(sexlabel.frame) + 80 * px, sexlabel.width, 60 * px);
    [self.view addSubview:Nationlabel];
    
    
    //民族
    UITextField * NationField = [[UITextField alloc] init];
    NationField.frame = CGRectMake(CGRectGetMaxX(Nationlabel.frame) + 20 * px, Nationlabel.y, ScreenWidth, Nationlabel.height);
    NationField.font = [UIFont systemFontOfSize:49 * px];
    NationField.enabled = NO;
    NationField.text = self.nationality;
    NationField.textColor = WYCRGBColor(120, 112, 115);
    [self.view addSubview:NationField];
    self.NationField = NationField;
    
    
    /******************出生********/
    UILabel * birthlabel = [[UILabel alloc] init];
    birthlabel.text = @"出生:";
    birthlabel.font = [UIFont systemFontOfSize:49 * px];
    birthlabel.textColor = WYCRGBColor(120, 112, 115);
    birthlabel.frame = CGRectMake(iphoneimage.x, CGRectGetMaxY(Nationlabel.frame) + 80 * px, nameLbel.width, 60 * px);
    [self.view addSubview:birthlabel];


    //出生
    UITextField * birthField = [[UITextField alloc] init];
    birthField.frame = CGRectMake(CGRectGetMaxX(birthlabel.frame) + 20 * px, birthlabel.y, ScreenWidth, birthlabel.height);
    birthField.font = [UIFont systemFontOfSize:49 * px];
    birthField.enabled = NO;
    NSArray *endarray = [self.birth componentsSeparatedByString:@" "];
    birthField.text = [NSString stringWithFormat:@"%@",endarray[0]];
    birthField.textColor = WYCRGBColor(120, 112, 115);
    [self.view addSubview:birthField];
    self.monthTexField = birthField;
    
    /******************住址********/
    UILabel * addresslabel = [[UILabel alloc] init];
    addresslabel.text = @"住址:";
    addresslabel.font = [UIFont systemFontOfSize:49 * px];
    addresslabel.textColor = WYCRGBColor(120, 112, 115);
    addresslabel.frame = CGRectMake(iphoneimage.x, CGRectGetMaxY(birthlabel.frame) + 80 * px, nameLbel.width, 60 * px);
    [self.view addSubview:addresslabel];
    
    
    //住址
    UITextField * addressField = [[UITextField alloc] init];
    addressField.frame = CGRectMake(CGRectGetMaxX(addresslabel.frame) + 10 * px, addresslabel.y, ScreenWidth, addresslabel.height);
    addressField.font = [UIFont systemFontOfSize:49 * px];
    addressField.text = self.address;
    addressField.enabled = NO;
    addressField.textColor = WYCRGBColor(120, 112, 115);
    [self.view addSubview:addressField];
    self.adressTexField = addressField;
    
    
    /******************证件号********/
    
    UILabel * numlabel = [[UILabel alloc] init];
    numlabel.text = @"号码:";
    numlabel.font = [UIFont systemFontOfSize:49 * px];
    numlabel.textColor = WYCRGBColor(120, 112, 115);
    numlabel.frame = CGRectMake(iphoneimage.x, CGRectGetMaxY(addresslabel.frame) + 80 * px, nameLbel.width, 60 * px);
    [self.view addSubview:numlabel];
    
    
    //号码
    UITextField * numField = [[UITextField alloc] init];
    numField.frame = CGRectMake(CGRectGetMaxX(numlabel.frame) + 20 * px, numlabel.y, ScreenWidth, numlabel.height);
    numField.font = [UIFont systemFontOfSize:49 * px];
    numField.text = self.num;
    numField.textColor = WYCRGBColor(120, 112, 115);
    [self.view addSubview:numField];
    self.numberTexField = numField;
    
    //登录按钮
    UIButton * loginButton = [[UIButton alloc] init];
    loginButton.frame = CGRectMake(iphoneimage.x, CGRectGetMaxY(numlabel.frame) + 80 * px, ScreenWidth - iphoneimage.x * 2, 150 * px);
    loginButton.backgroundColor = wycMain;
    loginButton.layer.cornerRadius = 10 * px;
    [loginButton setTitle:@"确认并保存" forState:UIControlStateNormal];
    loginButton.titleLabel.font = [UIFont systemFontOfSize:72 * px];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];
    
    
    
}
//保存按钮事件

- (void)loginClick {
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    NSDictionary * jsonStr = @{@"userId":[WYCAccountTool unarchiveuserId],@"name":self.nameTexField.text,@"sex":self.sexTexField.text,@"nationality":self.NationField.text,@"birth":self.monthTexField.text,@"address":self.adressTexField.text,@"num":self.numberTexField.text,@"faceUrl":self.faceUrl};
    
    NSError *error;
    
    NSData * data = [NSJSONSerialization dataWithJSONObject:jsonStr options:NSJSONWritingPrettyPrinted error:&error];
    
    NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSString *pas = [RSAUtil encryptString:jsonString publicKey:RSA_P_KEY];
    params[@"ticket"] = [WYCAccountTool unarchiveticket];;
    params[@"request"] = pas;
    [HttpTool POST:saveIdCardface params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([[responseObject objectForKey:@"code"] integerValue] == 0) {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"保存成功"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"ID" object:nil];
            
            UIViewController *vc =self.presentingViewController;
            
            //ReadBookController要跳转的界面
            
            while (![vc isKindOfClass:[WYCRealnameController class]]) {
                
                
                
                vc = vc.presentingViewController;
                
            }
            
            
            
            [vc dismissViewControllerAnimated:YES completion:nil];
        }else{
            
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            
    }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
         [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
    }];

    
}
//返回按钮
- (void)backClick{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_nameTexField resignFirstResponder];
    [_sexTexField resignFirstResponder];
    [_monthTexField resignFirstResponder];
    [_adressTexField resignFirstResponder];
    [_numberTexField resignFirstResponder];
    [_NationField resignFirstResponder];
}
@end
